# -*- coding:utf-8 -*-
import os
import platform
import hashlib


class myUtils:
    @staticmethod
    def removesubstr(string, substring):
        lists = string.split(",")
        for i in lists:
            if substring == i.rstrip():
                lists.remove(i)
        lists = ",".join(lists)
        return lists

    # about file system

    @staticmethod
    def removedir(dirpath):
        if os.path.exists(dirpath):
            # get abstract path
            dirpath = os.path.abspath(dirpath)
            if os.path.isfile(dirpath):
                os.remove(dirpath)
            else:
                filelist = os.listdir(dirpath)
                for f in filelist:
                    myUtils.removedir(dirpath + "/" + f)
                os.rmdir(dirpath)
            return True
        else:
            return False

    """
    some method about path
    """

    @staticmethod
    def somepathmethod(filepath):
        currentdir = os.getcwd()
        filename = os.path.basename(filepath)
        abstractfilepath = os.path.abspath(filepath)
        currentsystem = platform.system()
        print currentdir, filename, abstractfilepath, currentsystem

    @staticmethod
    def getmd5sum(filepath):
        if os.path.exists(filepath) and os.path.isfile(filepath):
            md5obj = hashlib.md5()
            md5obj.update(open(filepath, 'rb').read())
            return str(md5obj.hexdigest())


    @staticmethod
    def getfilemodtime(filename, path):
        mtime = time.localtime(os.stat(path + filename).st_ctime)
        timestr = time.strftime("%Y%m%d%H%M", mtime)
        return timestr

    @staticmethod
    def unzip(filepath):
        resultpath = filepath
        if os.path.exists(filepath) and os.path.isfile(filepath):
            resultpath = os.path.dirname(os.path.abspath(filepath)) + "/" + os.path.basename(
                filepath).split(r".")[0] + "src" + time.strftime("%Y%m%d%H%M%S",
                                                                 time.localtime()) + str(random.randint(0, 100))
            if filepath.endswith(".jar"):
                if platform.system() == "Linux":
                    commands.getstatusoutput("unzip " + filepath + " -d " + resultpath)
                else:
                    zipfile.ZipFile(filepath).extractall(resultpath)
            elif filepath.endswith(".tar.gz") or filepath.endswith(".tar"):
                t = tarfile.open(filepath, "r")
                t.extractall(path=resultpath)
                t.close()
        return resultpath
'''
        if logpath is not None and logpath != '':
            if not os.path.exists(logpath):
                with open(logpath, "w") as f:
                    f.close()
            logging.basicConfig(level=logging.DEBUG,
                                format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                                datefmt='%a, %d %b %Y %H:%M:%S',
                                filename=logpath,
                                filemode='w')
'''
